package ipad.asia.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import ipad.asia.bean.AlipayBean;
import ipad.asia.config.PropertiesConfig;

import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;


public class AlipayUtil {

    public static String connect(AlipayBean alipayBean) throws AlipayApiException {
        //1、获得初始化的AlipayClient
        AlipayClient alipayClient = new DefaultAlipayClient(
                PropertiesConfig.getKey("gatewayUrl"),//支付宝网关
                PropertiesConfig.getKey("app_id"),//appid
                PropertiesConfig.getKey("merchant_private_key"),//商户私钥
                "json",
                PropertiesConfig.getKey("charset"),//字符编码格式
                PropertiesConfig.getKey("alipay_public_key"),//支付宝公钥
                PropertiesConfig.getKey("sign_type")//签名方式
        );
        //2、设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        //页面跳转同步通知页面路径
        alipayRequest.setReturnUrl("http://49.235.177.225/pay/return_url");
        // 服务器异步通知页面路径
        alipayRequest.setNotifyUrl("http://49.235.177.225/pay/notify_url");
        //封装参数
        alipayRequest.setBizContent(JSON.toJSONString(alipayBean));
        String result = alipayClient.pageExecute(alipayRequest).getBody();
//        Map<String ,Object> bizContent=new HashMap<>();
//        bizContent.put("out_trade_no",out_trade_no);

        //返回付款信息
        return result;
    }
}
